/*
 * Copyright (c) 2025 Industrial Software Feature Database
 */
package com.comac.ins.isfd.domain.excel;

import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.write.merge.AbstractMergeStrategy;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;

public class CustomMergeStrategy extends AbstractMergeStrategy {
    @Override
    protected void merge(Sheet sheet, Cell cell, Head head, Integer relativeRowIndex) {
        Workbook workbook = sheet.getWorkbook();

        // 创建加粗字体
        Font boldFont = workbook.createFont();
        boldFont.setBold(true);

        // 创建表头样式
        CellStyle headerStyle = workbook.createCellStyle();
        headerStyle.setFont(boldFont);
        headerStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
        headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        headerStyle.setAlignment(HorizontalAlignment.CENTER);

        // 创建普通输入区域样式
        CellStyle inputStyle = workbook.createCellStyle();
        inputStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
        inputStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        inputStyle.setBorderBottom(BorderStyle.THIN);
        inputStyle.setBorderTop(BorderStyle.THIN);
        inputStyle.setBorderLeft(BorderStyle.THIN);
        inputStyle.setBorderRight(BorderStyle.THIN);

        int rowIndex = cell.getRowIndex();
        int columnIndex = cell.getColumnIndex();

        // 合并第一行A1-F1并设置样式
        if (rowIndex == 0 && columnIndex == 0) {
            sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 5));
            cell.setCellStyle(headerStyle);
        }

        // 第二行处理（取消合并，单独设置每个单元格）
        if (rowIndex == 1) {
            // A2单元格 - 定义X轴名称
            if (columnIndex == 0) {
                cell.setCellValue("定义X轴名称");
                cell.setCellStyle(headerStyle);
            }
            // B2单元格 - 留空，白色背景
            else if (columnIndex == 1) {
                cell.setCellValue("");
                cell.setCellStyle(inputStyle);
            }
            // C2单元格 - 定义Y轴名称
            else if (columnIndex == 2) {
                cell.setCellValue("定义Y轴名称");
                cell.setCellStyle(headerStyle);
            }
            // D2单元格 - 留空，白色背景
            else if (columnIndex == 3) {
                cell.setCellValue("");
                cell.setCellStyle(inputStyle);
            }
            // E2和F2单元格 - 留空
            else if (columnIndex == 4 || columnIndex == 5) {
                cell.setCellValue("");
                cell.setCellStyle(inputStyle);
            }
        }

        // 设置第三行表头样式
        if (rowIndex == 2) {
            cell.setCellStyle(headerStyle);
        }
    }
}
